const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors());
const port = 3000;
const host = 'localhost';

const CONTINENTALS = ['亚洲', '美洲', '大洋洲', '非洲', '欧洲'];
const COORDINATES = [
  [113.53, 22.28],
  [114.1, 22.2],
  [101.24, 2.57],
  [79.52, 6.55],
  [158.42, 19.99],
  [51.57, 1.21],
  [139.39, 35.27],
];

function randomBoolean() {
  return Math.random() > 0.5 ? true : false;
}

function randomIndex(n) {
  return Math.floor(Math.random() * n);
}

function randomInt(n) {
  return Math.round(Math.random() * n);
}

app.get('/airline/time', (req, res) => {
  res.status(200).send('' + Date.now());
});

app.get('/airline/entryExit', (req, res) => {
  res.status(200).send({
    flightCount: {
      enter: randomInt(10000),
      exit: randomInt(10000),
    },
    people: {
      enter: randomInt(10000),
      exit: randomInt(10000),
    },
    passengers: {
      enter: randomInt(10000),
      exit: randomInt(10000),
    },
    staffs: {
      enter: randomInt(10000),
      exit: randomInt(10000),
    },
  });
});

/**
 * @returns
 * {
 *  pvg: boolean,
 *  airbus: boolean,
 *  exit: boolean,
 *  coordinates: Array<number>,
 *  portName: string,
 *  continental: string
 * }
 */
app.get('/airline/getFlightLines', (req, res) => {
  const query = req.query;
  let n = 3,
    result = [],
    continental =
      query.continental || CONTINENTALS[randomIndex(CONTINENTALS.length)],
    pvgStr = '';
  for (let i = 0; i < n; i++) {
    if (query.airport === '' || typeof query.airport === 'undefined') {
      pvgStr = randomBoolean();
    } else {
      pvgStr = query.airport === 'PVG' ? true : false;
    }

    result.push({
      pvg: pvgStr,
      airbus: randomBoolean(),
      exit: randomBoolean(),
      portName: '大都会机场',
      coordinates: [-180 + Math.random() * 360, -180 + Math.random() * 360],
      continental,
    });
  }
  result = result.concat(
    result.map((r) => {
      return {
        ...r,
        exit: !r.exit,
      };
    })
  );

  res.status(200).send(result);
});

app.get('/airline/continentalFlights', (req, res) => {
  const obj = {
    china: [
      {
        name: '中国香港',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '中国澳门',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '中国台湾',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
    ],
    global: [
      {
        name: '亚洲',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '欧洲',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '非洲',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '大洋洲',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
      {
        name: '美洲',
        entry: 100 + randomInt(100),
        exit: 100 + randomInt(100),
      },
    ],
  };
  res.status(200).send(obj);
});

app.get('/airline/continental', (req, res) => {
  res.status(200).send({
    lines: randomInt(100),
    flights: randomInt(100),
    nations: [
      '英国',
      '美国',
      '柬埔寨',
      '缅甸',
      '老挝',
      '越南',
      '波斯尼亚和黑塞哥维那',
      '英属马尔维纳斯',
      '法属圭亚那',
      '俄罗斯',
      '法国',
      '毛里求斯',
      '印度尼西亚',
    ].sort((a, b) => (Math.random() > 0.5 ? -1 : 1)),
  });
});

const FLIGHT_NUMBER = ['CZ3095', 'NX135', 'DL283', 'MU760'];
const INTER_AIRPORT = [
  '台北桃园机场',
  '澳门国际机场',
  '大都会机场',
  '金边机场',
];

app.get('/airline/airlinedetail', (req, res) => {
  const query = req.query;

  let result = [];
  for (let i = 0; i < 100; i++) {
    let chnAirport = Math.random() > 0.5 ? '浦东国际机场' : '虹桥国际机场';
    if (query.airport) {
      chnAirport = query.airport === 'PVG' ? '浦东国际机场' : '虹桥国际机场';
    }

    let dest = INTER_AIRPORT[randomIndex(INTER_AIRPORT.length)],
      dep = chnAirport;
    if (query.crbz && +query.crbz === 2) {
      [dep, dest] = [dest, dep];
    }
    result.push({
      flightNumber: FLIGHT_NUMBER[randomIndex(FLIGHT_NUMBER.length)],
      departure: dep,
      destination: dest,
      arrivalTime: '20201029001300',
      type: randomBoolean() ? 'PAX' : 'CGO',
    });
  }
  res.status(200).send(result);
});

app.listen(port, host, () => {
  console.log(`listening on http://${host}:${port}`);
});
